package com.tal.pad.javabase;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.logging.Logger;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {

    private static volatile Semaphore limits = new Semaphore(1,true);

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i = 0 ; i< 10; i++){
            Thread thread = new Thread(new RunImpl());
            thread.setName("thread-test-"+i);
            executorService.submit(thread);
        }
    }

    static  class RunImpl implements  Runnable{

        @Override
        public void run() {
            System.out.println("RunImpl "+ Thread.currentThread().getName() + " start acquire");
            try {
                limits.acquire();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }finally {
                limits.release();
                System.out.println("RunImpl "+ Thread.currentThread().getName() + " end  release ");
            }
        }
    }

}